package com.service.Impl;

import com.dao.UserDao;
import com.entity.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    //登录
    public int login(String username,String password){
        User user = userDao.SelectUserByUsername(username);

        if (user==null){
            //不存在
            return 0;
        }
        if (user.getPassword().equals(password)){
            //成功
            return 1;
        }else {
            //密码错误
            return 2;
        }

    }

    //注册
    public int register(String username,String password1,String password2,String touxiang){
        if (!password1.equals(password2)){
            //密码不一致
            return 2;
        }
        User user = userDao.SelectUserByUsername(username);
        if (user!=null){
            //用户已存在
            return 3;
        }
        int i = userDao.addUser(username, password1,touxiang);
        return i;
    }

    //修改密码
    public int updPassword(String username,String oldPassword,String newPassword1,String newPassword2){
        User user = userDao.SelectUserByUsername(username);
        if (!user.getPassword().equals(oldPassword)){
            //密码错误
            return 2;
        }
        if (!newPassword1.equals(newPassword2)){
            //密码不一致
            return 3;
        }
        int i = userDao.updPassword(username, newPassword1);
        return i;
    }
}
